package com.spc.car.data.service.sys.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.spc.car.data.constant.CommConstants;
import com.spc.car.data.entity.sys.SysAttachment;
import com.spc.car.data.mapper.sys.SysAttachmentMapper;
import com.spc.car.data.service.sys.SysAttachmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 系统附件表 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-03-17
 */
@Service
@Slf4j
public class SysAttachmentServiceImpl extends ServiceImpl<SysAttachmentMapper, SysAttachment> implements SysAttachmentService {


    private final static String FILE_CODE = "CS";


    @Override
    public void saveAttach(SysAttachment attachment) {
        attachment.setStatus(CommConstants.FileConstants.FILE_STATUS_NORMAL);
        attachment.setCreateTime(new Date());
        save(attachment);
    }

    @Override
    public void saveAttachBatch(List<SysAttachment> attachments) {
        if (!CollectionUtils.isEmpty(attachments)) {
            List<SysAttachment> attachmentList = Lists.newArrayList();
            for (SysAttachment attachment : attachments) {
                attachment.setStatus(CommConstants.FileConstants.FILE_STATUS_NORMAL);
                attachment.setCreateTime(new Date());

                attachmentList.add(attachment);
            }
            saveOrUpdateBatch(attachmentList, 100);

        }
    }

    @Override
    public SysAttachment getAttachById(@NotNull Long attachId) {
        return getOne(new LambdaQueryWrapper<SysAttachment>()
                .eq(SysAttachment::getId, attachId));
    }


    @Override
    public void removeAttache(@NotNull Long attachId) {
        SysAttachment attachment = getAttachById(attachId);
        attachment.setStatus(CommConstants.FileConstants.FILE_STATUS_DEL);
        updateById(attachment);
    }
}
